package com.ruoyi.web.controller.contarctbill;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.enums.SystemStatusType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.contarct.domain.HouseContarct;
import com.ruoyi.contarct.service.IHouseContarctService;
import com.ruoyi.contarctbill.dto.CustomBillDTO;
import com.ruoyi.income.domain.HouseContarctIncome;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.contarctbill.domain.HouseContarctBill;
import com.ruoyi.contarctbill.service.IHouseContarctBillService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 账单Controller
 * 
 * @author hct
 * @date 2022-02-16
 */
@RestController
@RequestMapping("/contarctbill/contarctbill")
public class HouseContarctBillController extends BaseController
{
    @Autowired
    private IHouseContarctBillService houseContarctBillService;

    @Autowired
    private IHouseContarctService houseContarctService;

    /**
     * 查询账单列表
     */
    @PreAuthorize("@ss.hasPermi('contarctbill:contarctbill:list')")
    @GetMapping("/list")
    public TableDataInfo list(HouseContarctBill houseContarctBill)
    {
        String billMonth = houseContarctBill.getBillMonth();
        if(StringUtils.isNotEmpty(billMonth)){
            //根据月份获取账单时间区间
            Date start = null;
            Date end = null;
            try {
                Date billMonthDate = DateUtils.dateTime("yyyy-MM", DateUtils.getMth());
                if(StringUtils.isNotEmpty(billMonth)){
                    billMonthDate = DateUtils.dateTime("yyyy-MM", billMonth);

                }
                start = DateUtils.currMonthFirst(billMonthDate,"yyyy-MM-dd hh:mm:ss");
                end = DateUtils.currMonthLast(billMonthDate,"yyyy-MM-dd hh:mm:ss");
            } catch (ParseException e) {
            }
            houseContarctBill.setContarctPlanStartdate(start);
            houseContarctBill.setContarctPlanEnddate(end);
        }

        startPage();
        houseContarctBill.setStatus(Long.parseLong(SystemStatusType.ENABLE.getCode()+""));
        List<HouseContarctBill> houseContarctBills =  houseContarctBillService.selectHouseContarctBillListBy(houseContarctBill);
        for (HouseContarctBill bill : houseContarctBills
        ) {
            BigDecimal billShould = new BigDecimal(0D);//应收
            BigDecimal billAlready = new BigDecimal(0D);//已收
            List<HouseContarctIncome> houseContarctIncomes = bill.getHouseContarctIncomes();
            if (houseContarctIncomes != null && houseContarctIncomes.size() > 0) {
                for (HouseContarctIncome icome : houseContarctIncomes
                ) {
                    if(icome != null && icome.getFee() != null && icome.getYsfee() != null){
                        billShould = billShould.add(icome.getFee());
                        billAlready = billAlready.add(icome.getYsfee());
                    }
                }
            }
            bill.setBillShould(billShould);
            bill.setBillAlready(billAlready);

        }
        return getDataTable(houseContarctBills);
    }

    /**
     * 导出账单列表
     */
    @PreAuthorize("@ss.hasPermi('contarctbill:contarctbill:export')")
    @Log(title = "账单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseContarctBill houseContarctBill)
    {
        List<HouseContarctBill> list = houseContarctBillService.selectHouseContarctBillList(houseContarctBill);
        ExcelUtil<HouseContarctBill> util = new ExcelUtil<HouseContarctBill>(HouseContarctBill.class);
        util.exportExcel(response, list, "账单数据");
    }

    /**
     * 获取账单详细信息
     */
    @PreAuthorize("@ss.hasPermi('contarctbill:contarctbill:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(houseContarctBillService.selectHouseContarctBillById(id));
    }

    /**
     * 新增账单
     */
    @PreAuthorize("@ss.hasPermi('contarctbill:contarctbill:add')")
    @Log(title = "账单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseContarctBill houseContarctBill)
    {
        return toAjax(houseContarctBillService.insertHouseContarctBill(houseContarctBill));
    }

    /**
     * 修改账单
     */
    @PreAuthorize("@ss.hasPermi('contarctbill:contarctbill:edit')")
    @Log(title = "账单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseContarctBill houseContarctBill)
    {
        return toAjax(houseContarctBillService.updateHouseContarctBill(houseContarctBill));
    }

    /**
     * 删除账单
     */
    @PreAuthorize("@ss.hasPermi('contarctbill:contarctbill:remove')")
    @Log(title = "账单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(houseContarctBillService.deleteHouseContarctBillByIds(ids));
    }


    @PreAuthorize("@ss.hasPermi('contarctbill:contarctbill:list')")
    @PostMapping("/list")
    public AjaxResult currMonthBill(){
        try {
            Object currMonthBiiList = houseContarctBillService.getCurrMonthBiiList();
            return AjaxResult.success("",currMonthBiiList);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('contarctbill:contarctbill:add')")
    @Log(title = "添加自定义账单", businessType = BusinessType.INSERT)
    @PostMapping("/addCustomBill")
    public AjaxResult addCustomBill(@RequestBody CustomBillDTO customBillDTO){
        try {
            Object o = houseContarctBillService.addCustomBill(customBillDTO, getUsername());
            return AjaxResult.success("添加成功",o);
        } catch (Exception e) {
            return AjaxResult.error("添加失败");
        }
    }

}
